const DEFPIC = {
	"DEFHEADPIC": "images/defaultT.png",
	"DEFPIC": "images/defpic.png",
	"DEFPIC2": "images/defpic2.png",
}
// 封装加载图片方法
function loadImg(id, src) {
	if (!src) {
		return;
	}
	var tag = $('.' + id);
	var img = new Image();
	img.onload = function () {
		tag.each(function (index, el) {
			el.src = src
		});
	}
	img.src = src;
}

function loadImgList(data) {
	this.data = data;
	var tag = [];
	var img = [];
	for (var i = 0; i < this.data.length; i++) {
		tag[i] = $('.' + this.data[i][0]);
		img[i] = new Image();
		var src = this.data[i][1];

		img[i].onload = function () {
			tag[i].src = src;
		}
		img.src = src;
	}
}
// 设置本地存储
function setLocalStorage(distime, storageName, key, value) {
	// data用来存储过期时间
	distime = (new Date()).getTime() + distime;
	if (storageName) {
		storageName = localStorage;
		storageName.setItem('date', distime);
		storageName.setItem(key, value);
	} else {
		localStorage.setItem('date', distime);
		localStorage.setItem(key, value);
	}
}
// 判断存储信息是否过期
function checkLocalStorage(storageName) {
	var now = (new Date()).getTime();
	var distime = storageName.date;
	if (now > distime) {
		return false;
	}
	return true;
}

// 设置cockie
function setCookie(name, value, day = 0) {
	if (day !== 0) { //当设置的时间等于0时，不设置expires属性，cookie在浏览器关闭后删除
		var expires = day * 24 * 60 * 60 * 1000;
		var date = new Date(+new Date() + expires);
		document.cookie = name + "=" + escape(value) + ";expires=" + date.toUTCString();
	} else {
		document.cookie = name + "=" + escape(value);
	}
};
// 读取cookie
function getCookie(name) {
	var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
	if (arr = document.cookie.match(reg))
		return unescape(arr[2]);
	else
		return null;
}

// 格式化时间
function dateFormat(strtime, format = 'yyyy-mm-dd') {
	var strtime = strtime;
	this.formart = format;
	var datelist = strtime.split(' ')[0].split('-');
	var timelist = strtime.split(' ')[1].split(':');
	var date = {
		year: datelist[0],
		month: datelist[1],
		date: datelist[2]
	};
	var date2 = {
		year: datelist[0],
		month: datelist[1],
		date: datelist[2],
		hours: timelist[0],
		minutes: timelist[1],
		seconds: timelist[2]
	};
	if (format.toLowerCase() == 'yyyy-mm-dd hh:mm:ss') {
		return date2;
	} else {
		return date;
	}
}

// 字符表情(在escapeHtml后使用)转码
function utf16toEntities(str) {
	var patt = /[\ud800-\udbff][\udc00-\udfff]/g; // 检测utf16字符正则
	str = str.replace(patt, function (char) {
		var H, L, code;
		if (char.length === 2) {
			H = char.charCodeAt(0); // 取出高位
			L = char.charCodeAt(1); // 取出低位
			code = (H - 0xD800) * 0x400 + 0x10000 + L - 0xDC00; // 转换算法
			return "&#" + code + ";";
		} else {
			return char;
		}
	});
	return str;
}

// 设置唯一字符串
function uuid(len, radix) {
	var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
	var uuid = [], i;
	radix = radix || chars.length;

	if (len) {
		// Compact form
		for (i = 0; i < len; i++) uuid[i] = chars[0 | Math.random() * radix];
	} else {
		// rfc4122, version 4 form
		var r;

		// rfc4122 requires these characters
		uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
		uuid[14] = '4';

		// Fill in random data.  At i==19 set the high bits of clock sequence as
		// per rfc4122, sec. 4.1.5
		for (i = 0; i < 36; i++) {
			if (!uuid[i]) {
				r = 0 | Math.random() * 16;
				uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
			}
		}
	}

	return uuid.join('');
}